// pages/home/home.js
let http = require('../../utils/api.js');
let domain = require('../../utils/api.js');
import {
    toData,
    myHttps
} from '../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hiddenLoadings: false,//主题loding的显示
        iconsize: 12,//搜索icon的大小
        list: [],
        showborder: true,
        showborders: false,
        show: false,
        value: '',
        hiddenLoading: false,//资讯loding的显示
        zixun: true,
        zhuti: false,
        pramas: '', //从搜索页面带来的参数
        searchContent: '', //点击历史搜索内容带过来的参数
        isFocus: '',
        ids: 0,//ids=0 点的咨询 ids=1 点的主题
        img: '',
        page: 1,
        size: 10,
        winHeight: "", //窗口高度
        currentTab: 0, //预设当前项的值
        scrollLeft: 0, //tab标题的滚动条位置,
        hideHeader: true,
        hideBottom: true,
        refreshTime: '', // 刷新的时间 
        loadMoreData: '加载更多……'
    },
    // 滚动切换标签样式
    switchTab: function (e) {
        this.setData({
            currentTab: e.detail.current
        });
        this.checkCor();
        if (e.detail.current == 0) {//文章
            this.clickzx()
        } else if (e.detail.current == 1) {//主题
            this.clickzt()
        } else if (e.detail.current == 2) {//媒体
            this.media()
        }
    },
    // 点击标题切换当前页时改变样式
    swichNav: function (e) {
        var cur = e.target.dataset.current;
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }

    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function () {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function (res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR - 80;
                console.log(calc)
                that.setData({
                    winHeight: calc
                });
            }
        });
        this.setData({
            pramas: options._q,
            domain: domain.domain
        })
        //获取之后走接口渲染页面
        this.searchfinish('essay', this.data.pramas, this.data.page, this.data.size)
    },
    scroll: function (event) {
        this.setData({
            scrollTop: event.detail.scrollTop
        });
    }, 
    // 下拉刷新
    refresh: function (e) {
        // console.log('下拉刷新')
        var that = this;
        setTimeout(function () {
            var date = new Date();
            that.setData({
                size: 10,
                page: 1,
                scrollTop: 0,
                refreshTime: date.toLocaleTimeString(),
                hideHeader: false
            })
            if (that.data.currentTab == 0) {//文章
                that.clickzx()
            } else if (that.data.currentTab == 1) {//主题
                that.clickzt()
            } else if (that.data.currentTab == 2) {//媒体
                that.media()
            }

        }, 300);
    },
    // 上拉加载更多
    loadMore: function () {
        var that = this;
        setTimeout(function () {
            that.setData({
                size: 10,
                page: that.data.page + 1,
            })
            if (that.data.ids == 0) {
                that.searchfinish('essay', that.data.pramas, that.data.page, that.data.size)
            } else if (that.data.ids == 1) {
                that.searchfinish('topic', that.data.pramas, that.data.page, that.data.size)
            }
        }, 300);
    },
    //添加关注 关注主题接口
    addgz(e) {
        let index = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id;
        console.log(id)
        let url = http.api.followtheme + id + '/fav';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            if (res.code == 1) {
                this.data.list[index].isFav = true
                this.data.list[index].fav_count = this.data.list[index].fav_count + 1
                this.setData({
                    list: this.data.list
                })
            }
        }).catch(err => { })
    },
    // 取消关注
    canclegz(e) {
        let index = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id;
        let url = http.api.delFollow + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE"
        }).then(res => {
            if (res.code == 1) {
                this.data.list[index].isFav = false
                this.data.list[index].fav_count = this.data.list[index].fav_count - 1
                this.setData({
                    list: this.data.list
                })
            }
        }).catch(err => { })
    },
    // 点击每条媒体 进入媒体主页
    mediaHome(e) {
        let pid = e.currentTarget.dataset.pid
        let id = e.currentTarget.dataset.id
        // console.log(id)
        if (pid == 1) {
            wx.navigateTo({
                url: '../../pages/mediaHome/mediaHome?id=' + id,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
            })
        }
    },
    //文章接口
    clickzx() {
        //   走搜索接口
        this.setData({
            list: [],
            pramas: this.data.pramas,
            ids: 0,
            hiddenLoading: false
        })
        // console.log(this.data.pramas)
        this.searchfinish('essay', this.data.pramas, 1, 10, 'click')
        this.setData({
            showborder: true,
            showborders: false,
            zixun: true,
            zhuti: false
        });

    },

    //主题接口 点击主题 
    clickzt() {
        this.setData({
            list: [],
            pramas: this.data.pramas,
            ids: 1,
            hiddenLoading: false
        })
        this.searchfinish('topic', this.data.pramas, 1, 10, 'click');
        this.setData({
            showborder: false,
            showborders: true,
            zixun: false,
            zhuti: true
        });
    },

    //主题接口 点击主题 
    media() {
        this.setData({
            list: [],
            pramas: this.data.pramas,
            ids: 1,
            hiddenLoading: false
        })
        this.searchfinish('media', this.data.pramas, 1, 10, 'click');
        this.setData({
            showborder: false,
            showborders: true,
            zixun: false,
            zhuti: true
        });
    },
    // 资讯接口 点击每一条 跳到详情
    goDetails(e) {
        let id = e.currentTarget.dataset.id;
        // 跳到详情
        wx.navigateTo({
            url: '../details/details?id=' + id
        });
    },
    // 点击每一项 跳到主题主页
    goTheme(e) {
        var id = e.currentTarget.dataset.id
        // console.log(id)
        // console.log(wx.getStorageSync('isFocus'))
        wx.navigateTo({
            url: '../themeHome/themeHome?id=' + id
        });
    },

    // 走搜索接口 
    searchfinish(types, obj, page, size, click) {
        let url = http.api.searchfinish + types + '?_q=' + obj + '&page=' + page + '&size=' + size;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                if (click) {
                    for (var i = 0; i < res.data.content.length; i++) {
                        res.data.content[i].pubdate = toData(res.data.content[i].pubdate, 1)
                        // this.data.list.push(res.data.content[i])
                        this.data.list = res.data.content
                    }

                } else {//不是点击过来的
                    for (var i = 0; i < res.data.content.length; i++) {
                        res.data.content[i].pubdate = toData(res.data.content[i].pubdate, 1)
                        this.data.list.push(res.data.content[i])
                    }
                }

                this.setData({
                    list: this.data.list,
                    hiddenLoading: true,
                    hideHeader: true
                })
                // console.log(this.data.list)
            }
        }).catch(err => { })
    },
    // 顶部搜索框 enter事件
    searchKeyword(e) {
        let val = e.detail.value
        this.setData({
            pramas: val
        })
        if (this.data.currentTab == 0) {
            this.searchfinish('essay', this.data.pramas, 1, 10, 'click')
            this.setData({
                showborder: true,
                showborders: false,
                zixun: true,
                zhuti: false
            });
        } else if (this.data.currentTab == 1) {
            this.searchfinish('topic', this.data.pramas, 1, 10, 'click');
            this.setData({
                showborder: false,
                showborders: true,
                zixun: false,
                zhuti: true
            });
        } else if (this.data.currentTab == 2) {
            this.searchfinish('media', this.data.pramas, 1, 10, 'click');
            this.setData({
                // showborder: false,
                // showborders: true,
                // zixun: false,
                // zhuti: true
            });
        }
    },
    // 点击分享图标弹窗 
    showss(e) {
        console.log(e)
        this.setData({
            img: e.currentTarget.dataset.img,
            title: e.currentTarget.dataset.title,
            content: e.currentTarget.dataset.content,
            // clicknumber: e.currentTarget.dataset.value,
            // clickindex: e.currentTarget.dataset.index,
            id: e.currentTarget.dataset.id,
            show: true
        });
    },
    // 弹窗取消的点击事件
    cancel() {
        this.setData({
            show: false
        });
    },
    // 分享 页面
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            // console.log(res)
            this.cancel()
        }
        // console.log(res)
        return {
            title: this.data.title,
            imageUrl: this.data.img[0],
            path: '/pages/details/details?id=' + this.data.id
        }

        this.setData({
            show: false
        });
    },

    // 弹窗分享朋友
    Share() {
        this.setData({
            show: false
        })
        wx.showLoading({
            title: '加载中',
        })
        setTimeout(function () {
            wx.hideLoading()
        }, 2000)
        wx.navigateTo({
            url: '../picture/picture?title=' + this.data.title + '&content=' + this.data.content + '&id=' + this.data.id,
        })
    }
})